package cn.com.pajx.pajx_visitor_v3.ui.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.pajx.facelibrary.utils.FaceConfigUtils;

import butterknife.BindView;
import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.base.BaseActivity;
import cn.com.pajx.pajx_visitor_v3.ui.view.dialog.CommonDialog;
import cn.com.pajx.pajx_visitor_v3.utils.CommonUtil;
import cn.com.pajx.pajx_visitor_v3.utils.LogUtils;

/**
 * 启动页
 */
@SuppressLint({"CustomSplashScreen", "NonConstantResourceId"})
public class SplashActivity extends BaseActivity {

    @BindView(R.id.tv_version)
    TextView tvVersion;

    @Override
    protected boolean hasHeadTitle() {
        return false;
    }

    @Override
    protected boolean isTimerStart() {
        return false;
    }

    @Override
    protected void beforeSetContentView() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @RequiresApi(api = Build.VERSION_CODES.R)
    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {
        checkPermissions();
        tvVersion.setText("V" + CommonUtil.getVersionName(mContext));
    }

    /**
     * 跳转到主界面
     */
    private void toMain() {
        startActivity(new Intent(mContext, MainActivity.class));
        finish();
    }

    private void turnToMain() {
        new Handler().postDelayed(this::toMain, 2000);
    }

    /**
     * 检查敏感权限
     */
    @RequiresApi(api = Build.VERSION_CODES.R)
    private void checkPermissions() {
        String[] STORAGE_CAMERA_PERMISSION = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
        initCheckPermissions(STORAGE_CAMERA_PERMISSION, 100, "获取存储及相机权限,用于人脸识别");
    }

    @RequiresApi(api = Build.VERSION_CODES.R)
    @Override
    protected void onPermissionsExcute() {
        super.onPermissionsExcute();
        initCheckStorageManager();
    }

    private void initCheckStorageManager() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
            showManagerStoreage();
        } else {
            //配置人脸识别的参数
            FaceConfigUtils.isConfigExit(mContext);
            FaceConfigUtils.initConfig();
            turnToMain();
        }
    }

    /**
     * 申请管理全部文件的权限
     */
    @RequiresApi(api = Build.VERSION_CODES.R)
    private void showManagerStoreage() {
        new CommonDialog(mContext)
                .setTitle("管理文件、媒体权限")
                .setMessage("打开文件、媒体存储权限，否则无法存取照片")
                .setChildClickListener(commonDialog -> {
                    commonDialog.dismiss();
                    //30版本需要管理所有文件权限
                    Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                    intent.setData(Uri.parse("package:" + getPackageName()));
                    startActivityForResult(intent, 400);
                })
                .show();
    }

    @Override
    protected String loadDeniedDes() {
        return "文件、媒体存储及相机权限，用于照片采集及人脸识别";
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 400) {
            initCheckStorageManager();
        }
    }

    @Override
    public void onError(String message, int code, String tag) {
        LogUtils.e(message);
    }

}
